<script lang="ts" setup>
import { reactive, ref, onMounted } from 'vue'
import { FormInstance } from 'element-plus'
import { useTable } from '@/hooks/useTable'
import { queryBox } from '@/api/cabinet'
import { queryBoxTableCol } from '@/api/cabinet/types/cabinet'
import { getToken } from '@/utils/cache/cookies'
import CabinetTable from '@/components/table/CabinetTable.vue'
import { CabinetBikeTablePageType } from '@/types/enum'
import GoButton from '@/components/basic/GoButton.vue'
import { useRouter } from 'vue-router'
defineOptions({
  name: 'CabinetList'
})

const form = reactive({
  boxCode: '',
  type: '0',
  batteryLevel: ''
})
const formRef = ref<FormInstance | null>(null)

const { data, loading, pagination, load, handleCurrentChange, handleSizeChange } =
  useTable<queryBoxTableCol>(queryBox, getToken() ?? '', () => form)
const router = useRouter()
const cabinetAMap = () => {
  router.push({
    name: 'CabinetAMap',
    query: {
      storeId: ''
    }
  })
}

const handleFormSearch = () => {
  load()
}

const reset = () => {
  formRef.value?.resetFields()
}
onMounted(() => {
  load()
})
</script>

<template>
  <div class="xy-container">
    <el-form :model="form" ref="formRef" inline>
      <el-form-item prop="boxCode" label="电柜编号">
        <el-input v-model="form.boxCode" placeholder="请输入电柜编号" />
      </el-form-item>
      <el-form-item prop="type" label="网络状态">
        <el-select v-model="form.type" style="width: 120px">
          <el-option label="全部" value="0" />
          <el-option label="联网" value="2" />
          <el-option label="断网" value="1" />
        </el-select>
      </el-form-item>
      <el-form-item prop="batteryLevel" label="电池电量(主控1号仓)">
        <el-select v-model="form.batteryLevel" style="width: 120px">
          <el-option label="全部" value="" />
          <el-option label=">20%" value="gt20" />
          <el-option label="<=20%" value="lte20" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleFormSearch">查询</el-button>
        <el-button plain @click="reset">重置</el-button>
      </el-form-item>
      <el-form-item style="float: right">
        <go-button icon-name="cabinet" @click="cabinetAMap">
          <template #text>
            <span>电柜地图</span>
          </template>
        </go-button>
      </el-form-item>
    </el-form>
    <cabinet-table :data="data" :loading="loading" :page="CabinetBikeTablePageType.CabinetList" />
    <div flex justify-end mt-20>
      <el-pagination
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"
        v-model:current-page="pagination.pageNum"
        v-model:page-size="pagination.pageSize"
        background
        :total="pagination.total"
        layout="total,prev,pager,next,jumper,sizes"
      />
    </div>
  </div>
</template>

<style scoped></style>
